$(function() {
	//初始化列表数据
	changeHeaderMenu(0);
	changeMenu(0, 0);
	data_sn = getUrlParam("sn");
	var fwsn = getUrlParam("fwsn");
	if(!fwsn) {
		fwsn = data_sn;
	}
	$('.sn').html(fwsn);
	$('.data_sn').html('待查询的网吧/企业设备的编号sn：' + fwsn);
	initTable();
	savehandle();
	saveUser();
});
function saveUser(){
	$('.edit-OK').click(function(){
		if(confirm("确定保存修改?")){
			var user = $('.user').val();
			if (!user) {
				$('#alertdiv').savehandle('setmsg', {msg : '请输入名称!',timer : 2000});
				return;
			}
			var email = $('.email').val();
			if (!email) {
				$('#alertdiv').savehandle('setmsg', {msg : '请输入邮箱!',timer : 2000});
				return;
			}
			var phone1 = $('.phone1').val();
			var phone2 = $('.phone2').val();
			var addr = $('.addr').val();
			var province = $('#province option:selected').html();
			var city = $('#city option:selected').html();
			if(city.indexOf('请选择') == -1)
				addr = city +"-"+ addr;
			if(province.indexOf('请选择') == -1)
				addr = province + "-" + addr;
			var data = {"sn": data_sn, "user":user,"phone1":phone1,
					    "phone2":phone2,"addr":addr,
					    "email":email
					    };
			$.getJSON('/fw/agent/agentDev_changeAgent.action', data, function(data) {
				if (data.success) {
					$('#alertdiv').savehandle('setmsg', {
						msg : '成功!',
						timer : 2000
					});
					location.reload([true]);
				} else {
					var msg = data.result;
					$('#alertdiv').savehandle('setmsg', {
						msg : msg.errors,
						timer : 2000
					});
				}
			});
		}
		
	});
}


var data_sn;

function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap : {},
		successMsg : "成功！",
		failMsg : "失败，请重试！",
		success : refresh
	});
}

/**
 * 刷新
 * 
 * @return {TypeName}
 */
function refresh() {
	$('#grid').grid('load', {data : {}});
}

function findBySn() {
	var sn = $('#f_sn').val();
	$('#grid').grid('load',{
		data:{
		'sn':sn,
			}
	});
}
/**
 * 初始化表格数据
 */
function initTable() {
	
	$.getJSON(
			'/fw/agent/deviceAuz_getlist.action?sn=' + data_sn,function(data){
				if(data.rows && data.rows.length) {
					data = data.rows[0];
					var tx = data.tx;
		        	tx = parseInt(tx);
		        	if(tx == null){
		        		tx = "暂无数据!";
		        	}else if(tx<1024){
		        		tx = tx+"K/s";
		        	}else {
		        		tx = tx/1024 
		        		tx = tx.toFixed(2)
		        		tx = tx+"M/s";
		        	}
		        	var rx = data.rx;
		        	rx = parseInt(rx);
		        	if(rx == null){
		        		rx = "暂无数据!";
		        	}else if(rx<1024){
		        		rx = rx+"K/s";
		        	}else {
		        		rx = rx/1024 
		        		rx = rx.toFixed(2)
		        		rx = rx+"M/s";
		        	}
		        	var wan1 = data.wanIp1;
		        	if(wan1 == "0.0.0.0"){
		        		wan1 = "暂无数据！";
		        	}
		        	var wan2 = data.wanIp2;
		        	if(wan2 == "0.0.0.0"){
		        		wan2 = "暂无数据！";
		        	}
		        	var wan3 = data.wanIp3;
		        	if(wan3 == "0.0.0.0"){
		        		wan3 = "暂无数据！";
		        	}
		        	$('.model').html(data.model);
//		        	$('.sn').html(data.sn);
		        	$('.sys_ver').html(data.sysVer);
					$('.ip').html(data.ip);
					$('.cpu').html(data.cpu+"%");
					$('.mem').html(data.mem+"%");
					$('.online').html(data.online);
					$('.tx').html(tx);
					$('.rx').html(rx);
					$('.rechargeLast').html(data.rechargeLast?data.rechargeLast.split('.')[0]:'暂无数据');
					$('.activeLast').html(data.activeLast.split('.')[0]);
					$('.expireTime').html(data.expireTime.split('.')[0]);
					$('.wan1').html(data.expireTime.split('.')[0]);
					$('.wanIp1').html(wan1);
					$('.wanIp2').html(wan2);
					$('.wanIp3').html(wan3);
					$('.port').html(data.port);
				}
			}
		);
	$.getJSON('/fw/agent/agentDev_findbySN.action', {
		'sn': data_sn
	},function(data){
		if(data.success && data.result)
			data = data.result;
		data = data.rows[0];
		var user = data.user;
		if(user){
			$('.user').val(user);
		} else {
			$('.user').val("暂无数据");
		}
		var phone1 = data.phone1;
		if(phone1){
			$('.phone1').val(phone1);
		} else {
			$('.phone1').val("暂无数据");
		}
		var phone2 = data.phone2;
		if(phone2){
			$('.phone2').val(phone2);
		} else {
			$('.phone2').val("暂无数据");
		}
		var email = data.email;
		if(email){
			$('.email').val(email);
		} else {
			$('.email').val("暂无数据");
		}
		var addr = data.addr;
		var addrs = addr.split('-');
		getCity(addrs[0], addrs[1]);
		$('.addr').val(addrs[2]);
	});
}

var data_citys;
function getCity(pid, cid){
	$.getJSON('/fw/pages/agent/js/citys.json').done(function(data){
		data_citys = data;
		var data = data.result.rows;
		var html = "";
		for ( var int = 0; int < data.length; int++) {
			var d = data[int];
			var province = d.mainCity;
			html += '<option class="province_o" value="' + d.mainCity + '">' + d.mainCity + '</option>';
		}
		$("#province").html(html);
		if(pid)
			$("#province option[value=" + pid + "]").attr('selected', 'selected');
		provinceChange(cid);
	});
}

function provinceChange(cid){
	var data = data_citys;
	var province = $('.province').val();
	var data = data.result.rows;
	var html = "";
	for (var int = 0; int < data.length; int++) {
		var d = data[int];
		if(province == d.mainCity){
			var city = d.citys;
			for ( var i = 0; i < city.length; i++) {
				html += '<option class="city_o" value="' + city[i].name + '">' + city[i].name + '</option>';         
			}
			break;
		}
	}
	$("#city").html(html);
	if(cid)
		$("#city option[value=" + cid + "]").attr('selected', 'selected');
}

function formatTime(t) {
	if(t && t.length >= 14)
		return t.substr(0, 4) + '-' + t.substr(4, 2) + '-' + t.substr(6, 2) + ' ' + t.substr(8, 2) + ':' + t.substr(10, 2) + ':' + t.substr(12, 2);
	return t;
}
function inCafeList() {
	location.href = "inCafeList.jsp";
}

function del(){
	var ids = $('#grid').grid('getSelectedIds');
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要删除的数据!',timer:2000});
		return;
	}else{
		var idsStr = ids.join(',');
		if(confirm("这些数据确定要删除数据？")){
			$.getJSON(
				'/fw/agent/agent_del.action',
				{
					'ids':idsStr
				},
				function(data){
					$('#alertdiv').savehandle('handle',data,2000);
					refresh();
				}
			);
		}
	}
}